package com.item;

import java.util.HashMap;
import java.util.Map;

public class MapPractice {
    public static void main(String[] args) {
        Map<String,String> data = new HashMap<>();
        data.put("小王1","A");
        data.put("小张1","B");
        data.put("小李1","C");
        data.put("小赵1","D");
        data.put("小王2","A");
        data.put("小王3","B");
        data.put("小王4","C");
        data.put("小王5","D");
        data.put("小王6","A");
        data.put("小王7","B");
        data.put("小王8","C");
        data.put("小王9","D");
        data.put("小张2","A");
        data.put("小张3","B");
        data.put("小张4","C");
        data.put("小李2","A");
        data.put("小李3","B");
        data.put("小李4","B");
        data.put("小赵2","C");
        data.put("小赵3","D");
        count(data);
    }
    public static void count(Map<String,String> data){
        /**
         * 1.创建新的Map集合，用来存储投票结果
         * 2.遍历data集合（key---value)
         * 3.获取value值，将value值作为key存在到新的Map集合中
         *  ---如果value值不存在，则将value---1存入新的Map集合中
         *  ---如果value值存在，则value对应的value+1
         */
        Map<String,Integer> resultMap = new HashMap<>();
        data.forEach((k,v)->{
//             获取value值，将value值作为key存在到新的Map集合中
//             如果value值不存在，则将value---1存入新的Map集合中
//             如果value值存在，则value对应的value+1
            boolean containsKey = resultMap.containsKey(v);
            if(containsKey){
                //已存在
                Integer oldvalue = resultMap.get(v);
                resultMap.put(v,oldvalue+1);
            }else {
                //不存在
                resultMap.put(v,1);
            }
        });
        resultMap.forEach((k,v)->{
            System.out.println(k+"---"+v);
        });
    }
}
